package com.baosight.sczx.zm.cl.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.zm.cl.domain.ZMCLReport_CivilizationProduce;
import com.baosight.sczx.zm.cl.domain.ZMCLReport_RiskRecord;
import com.baosight.sczx.zm.cl.domain.ZMCLReport_SafetyReport;

/**
 * 安全日报模块(数据维护)
 * 主表
 * <p>Title: ServiceZMCLR0.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 严洪浩
 * @version 1.0
 * <pre>Histroy:
 *          2011-11-18 上午08:56:07  严洪浩  Create
 *</pre>
 */
public class ServiceZMCLR0 extends ServiceEPBase {
	
	/**
	 * 安全隐患日报表 进入初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCL50</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-5-30              高强       
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo)
	{
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * 安全信息日报记录 查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *     2011-5-30               高强       
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo)
	{
		StaticTools.setQueryParameters(inInfo, "fdType", "0", true);//添加查询条件
		//StaticTools.setQueryParameters(inInfo, "fdCreateDepartmentId", UserSession.getInSessionProperty("orgCode"), true);//编制部门
		EiInfo outInfo = super.query(inInfo, "ZMCLReport_SafetyReport.query", new ZMCLReport_SafetyReport());
		return outInfo;
	}
	
	/** 新增隐患日报
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-5-30  高强
	 * </p>
	 *
	*/
	@Override
	public EiInfo insert(EiInfo inInfo) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("fdReportDate", inInfo.getBlock("result").getCellStr(0, "fdReportDate"));//日报日期
		params.put("fdType", inInfo.getBlock("result").getCellStr(0, "fdType"));//日报类型
		
		boolean validateValue = this.validateReport(inInfo, params);//判断日报是否存在
		if(validateValue) {
			return inInfo;
		}

		inInfo.setCell("result", 0, "fdKeyId", TablePk.getInstance().getNextValue("T_CL_REPORT_SAFETYREPORT"));//主键
		inInfo.setCell("result", 0, "fdCreatePeopleId", UserSession.getUserId());//编制人ID
		inInfo.setCell("result", 0, "fdCreatePeople", UserSession.getInSessionProperty("UserCname"));//编制人
		inInfo.setCell("result", 0, "fdCreateDepartmentId", UserSession.getInSessionProperty("orgCode"));//编制部门ID
		inInfo.setCell("result", 0, "fdCreateDepartment", UserSession.getInSessionProperty("orgName"));//编制部门
		inInfo.setCell("result", 0, "fdCreatetTime", StaticTools.getDateTime());//编制时间
		
		EiInfo outInfo = super.insert(inInfo, "ZMCLReport_SafetyReport.insert");
		return outInfo;
	}
    
	/** 修改隐患日报
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-5-30  高强
	 * </p>
	 *
	*/
	@Override
	public EiInfo update(EiInfo inInfo) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("fdKeyId", inInfo.getBlock("result").getCellStr(0, "fdKeyId"));//主键
		System.out.println("............... >> " + inInfo.getBlock("result").getCellStr(0, "fdKeyId"));
		params.put("fdReportDate", inInfo.getBlock("result").getCellStr(0, "fdReportDate"));//日报日期
		params.put("fdType", inInfo.getBlock("result").getCellStr(0, "fdType"));//日报类型
		
		boolean validateValue = this.validateReport(inInfo, params);//判断日报是否存在
		if(validateValue) {
			return inInfo;
		}

		inInfo.setCell("result", 0, "fdCreatePeopleId", UserSession.getUserId());//编制人ID
		inInfo.setCell("result", 0, "fdCreatePeople", UserSession.getInSessionProperty("UserCname"));//编制人
		inInfo.setCell("result", 0, "fdCreateDepartmentId", UserSession.getInSessionProperty("orgCode"));//编制部门ID
		inInfo.setCell("result", 0, "fdCreateDepartment", UserSession.getInSessionProperty("orgName"));//编制部门
		inInfo.setCell("result", 0, "fdCreatetTime", StaticTools.getDateTime());//编制时间
		
		EiInfo outInfo = super.insert(inInfo, "ZMCLReport_SafetyReport.update");
		return outInfo;
	}
	
	/**
	 * 更新发布状态为"生效"的方法
	 * <p>Function: publish</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *     2011-6-2  高强
	 * </p>
	 *
	 */
	public EiInfo publish(EiInfo inInfo) 
	{
		EiInfo _temp = super.update(inInfo, "ZMCLReport_SafetyReport.publish");
        EiInfo outInfo = this.query(_temp);
		return outInfo;
	}
	
	/**
	 * 安全信息日报 删除
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-2  高强
	 * </p>
	 *
	*/
	@Override
	public EiInfo delete(EiInfo inInfo)
	{
		Map<String, String> params = new HashMap<String, String>();
		params.put("fdReportId", inInfo.getBlock("result").getCellStr(0, "fdKeyId"));
		boolean validateValue = this.canDelete(inInfo, params);//从表 存在数据 不能删除
		if(validateValue) {
			return inInfo;
		}
		
		EiInfo _temp = super.delete(inInfo, "ZMCLReport_SafetyReport.delete");
		return query(_temp);
	}

	
	/**
	 * 检查是否有重复的日报存在
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-2  高强
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validateReport(EiInfo inInfo, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		List result = dao.query("ZMCLReport_SafetyReport.validate", params);
		if(result!=null && result.size() > 0) {
			inInfo.setStatus(-1);
			ZMCLReport_SafetyReport data = (ZMCLReport_SafetyReport)result.get(0);
			inInfo.setMsg("日期为" +data.getFdReportDate()+ "的日报已经存在！");
			return true;
		}
		return false;
	}
	
	/**
	 * 检查是否可以删除日报
	 * <p>Function: canDelete</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-2  高强
	 * </p>
	 *
	 */
	@SuppressWarnings("unchecked")
	private boolean canDelete(EiInfo inInfo, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}

		List<ZMCLReport_RiskRecord> resultRisk = dao.query("ZMCLReport_RiskRecord.query", params);
		if(resultRisk != null && resultRisk.size() > 0) {
			inInfo.setStatus(-1);
			ZMCLReport_RiskRecord data = (ZMCLReport_RiskRecord)resultRisk.get(0);
			inInfo.setMsg("日期为" +data.getFdRiskDate()+ "的日报下存在隐患项目不能删除！");
			return true;
		}
		
		List<ZMCLReport_CivilizationProduce> resultCivilization = dao.query("ZMCLReport_CivilizationProduce.query", params);
		if(resultCivilization != null && resultCivilization.size() > 0) {
			inInfo.setStatus(-1);
			ZMCLReport_CivilizationProduce data = (ZMCLReport_CivilizationProduce)resultCivilization.get(0);
			inInfo.setMsg("日期为" +data.getFdDate()+ "的日报下存在文明生产不能删除！");
			return true;
		}
		
		return false;
	}
}
